package com.glsc.ngateway.opmanage.service.outsys;


import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.outsys.OutSystemTemplate;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.outsys.OutSystemTemplateRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
@Slf4j
public class OutSystemTemplateService {

    @Resource
    private OutSystemTemplateRepository templateRepository;

    @Resource
    private OutSystemTemplateItemService outSystemTemplateItemService;

    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<OutSystemTemplate> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<OutSystemTemplate> page = templateRepository.findAll(SpecificationUtil.buildSpecification(param, OutSystemTemplate.class), pageable);
        return page;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public OutSystemTemplate save(OutSystemTemplate obj) {
        return templateRepository.save(obj);
    }

    /**
     * 根据地查找
     *
     * @param id
     * @return
     */
    public OutSystemTemplate findById(Integer id) {
        Optional<OutSystemTemplate> optional = templateRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 查询所有模板信息
     *
     * @return
     */
    public List<OutSystemTemplate> findAll() {
        return templateRepository.findAll();
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    @Transactional
    public void deleteById(Integer id) {
        templateRepository.deleteById(id);
    }


}
